package xxh;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.SQLException;

public class BorrowRecordUI extends JFrame {
    private BackgroundPanel topPanel; // 使用BackgroundPanel
    private JLabel lblUserNo;
    private JTextField txtUserNo;
    private DefaultTableModel tableModel;
    private JTable table;
    private JScrollPane scrollPane;
    private JButton btnSearch;

    public BorrowRecordUI(String Uno) {

        // 设置窗口属性
        setTitle("借阅记录查询");
        setSize(900, 600);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null); // 窗口居中显示

        // 创建自定义的背景面板

        topPanel = new BackgroundPanel("src/xxh/img/bk.jpg",600,150);
        topPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 30, 20));

        lblUserNo = new JLabel("用户编号:");
        lblUserNo.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        lblUserNo.setForeground(new Color(45, 45, 45));

        txtUserNo = new JTextField(15);
        txtUserNo.setText(Uno);
        if (Uno != null) txtUserNo.setEditable(false);
        txtUserNo.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        txtUserNo.setForeground(new Color(50, 50, 50));
        txtUserNo.setBackground(Color.WHITE);
        txtUserNo.setBorder(BorderFactory.createLineBorder(new Color(200, 200, 200), 1));  // 设置边框

        btnSearch = new JButton("查询");
        btnSearch.setFont(new Font("微软雅黑", Font.BOLD, 16));
        btnSearch.setBackground(new Color(0, 123, 255));
        btnSearch.setForeground(Color.WHITE);
        btnSearch.setFocusPainted(false);
        btnSearch.setPreferredSize(new Dimension(120, 40));
        btnSearch.setBorder(BorderFactory.createLineBorder(new Color(0, 105, 217), 2));

        topPanel.add(lblUserNo);
        topPanel.add(txtUserNo);
        topPanel.add(btnSearch);

        // 中间表格部分
        tableModel = new DefaultTableModel();
        table = new JTable(tableModel);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); // 自动调整列宽
        table.setRowHeight(30); // 设置行高
        table.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        table.setSelectionBackground(new Color(230, 230, 255)); // 选中行的背景色
        table.setSelectionForeground(Color.BLACK); // 选中行的字体颜色
        table.setGridColor(new Color(220, 220, 220)); // 设置表格网格颜色

        scrollPane = new JScrollPane(table);
        scrollPane.setBorder(BorderFactory.createLineBorder(new Color(200, 200, 200), 1));

        // 设置表头
        tableModel.setColumnIdentifiers(new String[]{"用户编号", "用户姓名", "书籍编号", "书名", "借阅时间", "归还时间"});
        table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 16)); // 设置表头字体
        table.getTableHeader().setBackground(new Color(245, 245, 245)); // 设置表头背景色
        table.getTableHeader().setForeground(new Color(60, 60, 60)); // 设置表头文字颜色

        // 添加组件到窗口
        add(topPanel, BorderLayout.NORTH);
        add(scrollPane, BorderLayout.CENTER);

        // 查询按钮点击事件
        btnSearch.addActionListener(e -> {
            String userNo = txtUserNo.getText().trim();
            BorrowRecordProcess borrowRecordProcess;

            try {
                borrowRecordProcess = new BorrowRecordProcess();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }

            try {
                borrowRecordProcess.loadBorrowRecords(tableModel, userNo.isEmpty() ? null : userNo);
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(this, "查询失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
            }
        });
    }
}
